using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace usm.SolDev.DataBase.DataObjects
{
    /// <summary>
    ///     The message type constants
    /// </summary>
    public enum MessageType
    {
        info = 1,
        warning = 2,
        error = 3
    }

    /// <summary>
    ///     The compiler's message line accepted by this class should 
    /// be formated like this:
    /// "Error 23 : Wring identifier"
    /// i.e. "<error_key> <line number> : <error message>"
    /// </summary>
    public class MessageLine
    {
        #region Private data
        private string messageText;
        private string rawMessage;
        private MessageType messageType;
        private int lineNumber;
        #endregion

        #region Constructors
        /// <summary>
        ///     Default constructor
        /// </summary>
        public MessageLine () {}

        /// <summary>
        ///     Initialization constructor
        /// </summary>
        /// <param name="mesageLine">the message line to process</param>
        public MessageLine(string mesageLine) 
        {
            ParseLine(mesageLine);
        }
        #endregion

        #region Helpers
        /// <summary>
        ///     Parse the compiler's message line   
        /// </summary>
        /// <param name="message">the message line</param>
        /// <returns>parse result</returns>
        public bool ParseLine(string message)
        {
            #region Declare
            Match matchResult = null;
            #endregion
            try
            {
                rawMessage = message;

                #region error
                matchResult = Regex.Match(message, @"(?i)error\s+(?<line>[^:]+):(?<message>.*)");
                if (matchResult != null && matchResult.Length > 0)
                {
                    messageType = MessageType.error;
                    lineNumber = int.Parse(matchResult.Result("${line}"));
                    messageText = matchResult.Result("${message}");
                    return true;
                }
                #endregion

                #region warning
                matchResult = Regex.Match(message, @"(?i)warning\s+(?<line>[^:]+):(?<message>.*)");
                if (matchResult != null && matchResult.Length > 0)
                {
                    messageType = MessageType.warning;
                    lineNumber = int.Parse(matchResult.Result("${line}"));
                    messageText = matchResult.Result("${message}");
                    return true;
                }
                #endregion
            
            }catch{}

            #region retrun in case of failure 
            messageType = MessageType.info;
            lineNumber = 0;
            messageText = message;
            return false;
            #endregion
        }
        #endregion

        #region Public properties
        /// <summary>
        ///     Selected message text
        /// </summary>
        public string MessageText
        {
            get { return messageText;}
        }

        /// <summary>
        ///     The original message
        /// </summary>
        public string RawMessage
        {
            get { return rawMessage;}
        }

        /// <summary>
        ///     The message type
        /// </summary>
        public MessageType MessageType
        {
            get { return messageType;}
        }

        /// <summary>
        /// The message's line number 
        /// </summary>
        public int LineNumber
        {
            get { return lineNumber;}
        }
        #endregion

    }
}
